﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace CMS
{
    public partial class Articleer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }
        public static DataTable data = new DataTable();

        private void FillData()
        {
            var sql = "select * from Articles";
            data = DbHelper.GetData(sql);
            ArtivleerGridView.DataSource = data;
            ArtivleerGridView.DataBind();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ArtivleerGridView.EditIndex = e.NewEditIndex;
            FillData();
        }
        /// <summary>
        /// 取消编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ArtivleerGridView.EditIndex = -1;
            FillData();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = AcquireCell(e.RowIndex,0);
            var sql = string.Format("delete Articles where Id = {0}",id);
            DbHelper.AddAndDeleteAddUpdate(sql);
            FillData();
        }
        /// <summary>
        /// 获取单元格
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        private string AcquireCell(int rowIndex, int cellIndex)
        {
            var cell = ArtivleerGridView.Rows[rowIndex].Cells[cellIndex];
            var value = cell.Controls.Count > 0?((TextBox)cell.Controls[0]).Text : cell.Text ;
            return value;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = AcquireCell(e.RowIndex, 0);
            var title = AcquireCell(e.RowIndex, 1);
            var content = AcquireCell(e.RowIndex, 2);
            var author = AcquireCell(e.RowIndex, 3);
            var remarks = AcquireCell(e.RowIndex, 4);

            var isOk = int.TryParse(idString, out int id);

            if (isOk)
            {
                var sql = string.Format("update Articles set Title = '{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id = {4}", title, content, author, remarks, id);
                DbHelper.AddAndDeleteAddUpdate(sql);
            }
            else
            {
                var sql = string.Format("insert into Articles(Title,Content,Author,Remarks) values('{0}','{1}','{2}','{3}')",title,content,author,remarks);
                DbHelper.AddAndDeleteAddUpdate(sql);
            }


            ArtivleerGridView.EditIndex = -1;
            FillData();

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddButton_Click(object sender, EventArgs e)
        {
            var copyData = data.Copy();
            copyData.Rows.Add(copyData.NewRow());
            data = copyData;

            ArtivleerGridView.DataSource = data;
            ArtivleerGridView.DataBind();

            ArtivleerGridView.EditIndex = data.Rows.Count - 1;
            ArtivleerGridView.DataSource = data;
            ArtivleerGridView.DataBind();
        }
    }
}